package com.ksyun.campus.client;

import java.io.IOException;
import java.io.InputStream;

public class FSInputStream extends InputStream {
    private byte[] bytes;

    private int pos;

    private int count;

    public FSInputStream(byte[] bytes) {
        this.bytes = bytes;
        this.pos = 0;
        this.count = bytes.length;
    }

    @Override
    public int read() {
        return (pos < count) ? (bytes[pos++] & 0xff) : -1;
    }

    @Override
    public int read(byte[] b) {
        return read(b, 0, b.length);
    }

    @Override
    public int read(byte[] b, int off, int len) {
        if (b == null) {
            throw new NullPointerException();
        } else if (off < 0 || len < 0 || len > b.length - off) {
            throw new IndexOutOfBoundsException();
        }
        if (pos >= count) {
            return -1;
        }
        int avail = count - pos;
        if (len > avail) {
            len = avail;
        }
        if (len <= 0) {
            return 0;
        }
        System.arraycopy(bytes, pos, b, off, len);
        pos += len;
        return len;
    }

    @Override
    public void close() throws IOException {
        super.close();
    }
}
